
package project3;

import java.awt.geom.Point2D;
import java.io.File;
import java.util.*;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * This class acts as the 'main', or host of the other modules of the program.
 * It is the primary user interface for our IR system.
 * @author JSteiner
 */
public class Interface extends javax.swing.JFrame {

	InformationRetreiver ir = new InformationRetreiver();
	String path;
    /** Creates new form Interface */
    public Interface() {
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        path = JOptionPane.showInputDialog("Enter a path to index");
		ir.index(new File(path));
        initComponents();
        fillCollectionVisualization();
    }

    private void fillCollectionVisualization()
    {
        Map<Document, Point2D.Double> docMap=new HashMap<Document, Point2D.Double>();
        for(Document d : ir.getDocuments())
        {
            Point2D.Double point=new Point2D.Double();
            point.x=d.getStemCount();
            point.y=d.getDocumentLengthInBytes();
            docMap.put(d, point);
        }
        CollectionVisualization.setMap(docMap);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        QueryVisualization = new MakingAGrid(null);
        CollectionVisualization = new GridDisplay();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                input(evt);
            }
        });

        jLabel1.setText("$Dave>");
        jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jLabel1.setOpaque(true);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 498, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(57, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Query", jPanel2);

        jScrollPane2.setViewportView(jList1);

        jButton1.setText("Open");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(62, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Query Return", jPanel1);

        javax.swing.GroupLayout VisLayout = new javax.swing.GroupLayout(QueryVisualization);
        QueryVisualization.setLayout(VisLayout);
        VisLayout.setHorizontalGroup(
            VisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 563, Short.MAX_VALUE)
        );
        VisLayout.setVerticalGroup(
            VisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 334, Short.MAX_VALUE)
        );
        jTabbedPane1.addTab("Query Visualization", QueryVisualization);

        javax.swing.GroupLayout CVisLayout = new javax.swing.GroupLayout(CollectionVisualization);
        CollectionVisualization.setLayout(CVisLayout);
        CVisLayout.setHorizontalGroup(
            CVisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 563, Short.MAX_VALUE)
        );
        CVisLayout.setVerticalGroup(
            CVisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 334, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Collection Visualization", CollectionVisualization);

        jMenu1.setText("File");

        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE))
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("Buttons");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void input(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_input
        String text = jTextField1.getText();
        
        if(text != null/* && !text.equals("")*/) {
             jTextArea1.append("$Usr\\Dave>" + text + "\n");
             jTextArea1.append("$Usr\\HAl> Searching...\n");
             input_helper(text);

             jTextField1.selectAll();
        }
        else {
             jTextArea1.append("$Usr\\HAl> I can't let you do that, Dave...\n");
        }

}//GEN-LAST:event_input

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
         java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
				SearchResult result = (SearchResult)jList1.getModel().getElementAt(jList1.getAnchorSelectionIndex());
				new FileDisplay(result.document).setVisible(true);
            }
        }); /*REPLACE WITH FILE PARALLEL*/
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
       this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void input_helper(final String text) {
    		/*Add goodness list here*/
    	    /*Stem here - invert - use InformationRetreiver - Goodness the resulting list*/
    		
			final List<SearchResult> files = ir.findInformation(text, true);
			HashMap<Document, double[]> m = buildMap(files);
			QueryVisualization.setDocuments((Map)m);
            jList1.setModel(new javax.swing.AbstractListModel() {
                public int getSize() { return files.size(); }
                public Object getElementAt(int i) { return files.get(i); }
        });

            jTextArea1.append("$Usr\\HAl> Found "+files.size()+" documents\n");
    }
	
	/** Builds a map of document -> 3 length vector points, for use by query visualizer. */
	private HashMap<Document, double[]> buildMap(List<SearchResult> results)
	{
        HashMap<Document, double[]> returnValue=new HashMap<Document, double[]>();
        for(SearchResult result : results)
		{
            double goodity = result.score;
            double stemCount = result.document.getStemIndexEntries().size();
            double[] vector = {goodity, goodity, stemCount};
            returnValue.put(result.document, vector);
        }
        return returnValue;
	}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Interface().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private MakingAGrid QueryVisualization;
    private GridDisplay CollectionVisualization;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private final javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
